package com.example;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CodeReturn
 * 입력 스트림을 통한 파일을 다운 로드 하는 코드 
 */
public class CodeReturn extends HttpServlet {
	private static final long serialVersionUID = 1L;
       /*
해당 경로에 파일이 없어서 에러나는 것 나중에 수정할경우 바꿀것*/
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/jar");
		ServletContext ctx = getServletContext();
		String path =		ctx.getContextPath();
		InputStream is = ctx.getResourceAsStream("/hsqldb.jar");
		System.out.println("이름 :"+ctx.getServletNames());
		System.out.println("패스"+path);
		System.out.println(ctx.getRealPath(path));
		int read = 0 ;
		byte[] bytes = new byte[1024];
		
		OutputStream os = response.getOutputStream();
		while((read = is.read(bytes)) != -1) {
			System.out.println(is.read(bytes));
			os.write(bytes, 0 , read);			
			
		}
		os.flush();
		os.close();
	}

}
